/// <reference path="jquery-vsdoc.js" />
/// <reference path="jquery-ui-1.8.custom.min.js" />

Object.count = function (aObject) {
    var i = 0;
    for (var item in aObject) {
        if (aObject[item]) i++;
    }
    return i;
};

// Based on Jquery
$.prototype.comboxBindingIn = function (data, displayField, valueField) {
    var selectControl = $("<select/>");
    $(data).each(function (index, item) {
        $('<option/>').val(item[valueField]).html(item[displayField]).appendTo(selectControl);
    });
    this.append(selectControl);
    return this;
};
$.prototype.comboxBinding = function (data, displayField, valueField) {
    var selectControl = this.empty();
    $(data).each(function (index, item) {
        $('<option/>').val(item[valueField]).html(item[displayField]).appendTo(selectControl);
    });
    return this;
};
$.prototype.comboxBindingWithEmptyDefault = function (data, displayField, valueField, defaultEmptyText) {
    var selectControl = this.empty();
    var emptyItem = $('<option/>');
    if (defaultEmptyText) {
        emptyItem.html(defaultEmptyText);
    }
    emptyItem.appendTo(selectControl);
    $(data).each(function (index, item) {
        $('<option/>').val(item[valueField]).html(item[displayField]).appendTo(selectControl);
    });
    return this;
};
$.prototype.comboxBindingWithArray = function (data) {
    var selectControl = this.empty();
    $(data).each(function (index, item) {
        $('<option/>')
          .val(item)
          .html(item)
          .appendTo(selectControl);
    });
    return this;
};

Date.prototype.toChineseDateFormat = function () {
    var dateMonth = this.getMonth() + 1;
    var dateDay = this.getDate();

    if (dateMonth < 10) {
        dateMonth = '0' + dateMonth;
    }
    if (dateDay < 10) {
        dateDay = '0' + dateDay;
    }

    if (arguments.length == 0)
        return this.getFullYear() + "-" + dateMonth + "-" + dateDay;
    switch (arguments[0]) {
        case "yyyy-MM":
            return this.getFullYear() + "-" + dateMonth;
    }
};
String.prototype.toDateTime = function () {
    if (this.indexOf(".") > 0) {
        var data = this.split('.');
        return new Date(data[0], data[1] - 1, data[2]);
    }
    else if (this.indexOf("-") > 0) {
        var data = this.split('-');
        return new Date(data[0], data[1] - 1, data[2]);
    }
    else if (this.indexOf("/") > 0) {
        var data = this.split('/');
        return new Date(data[2], data[0] - 1, data[1]);
    }
}
String.prototype.htmlEncode = function () {
    var div = document.createElement("div");
    var text = document.createTextNode(this);
    div.appendChild(text);

    return div.innerHTML;
}

///字符串匹配
String.prototype.startWith = function (str) {
    if (str == null || str == "" || this.length == 0 || str.length > this.length)
        return false;
    if (this.substr(0, str.length) == str)
        return true;
    else
        return false;
    return true;
}


//取出字符串头空格
String.prototype.trim = function () {
    var str = this;
    if (str.charAt(0) == " " || str.charAt(0) == "\n") {
        str = str.slice(1);
        str = Trim(str);
    }
    return str;
}

Date.prototype.toFullChineseDateFormat = function () {
    var yy = this.getFullYear();
    var mon = this.getMonth() + 1;
    var da = this.getDate();
    var day = this.getDay();

    var date = yy + "年";
    if (mon < 10) date += '0';
    date += mon + "月";
    if (da < 10) date += '0';
    date += da + "日";
    var date = date + "&nbsp星期" + "日一二三四五六".charAt(day);
    return date;
}
Date.prototype.toFullTimeFormat = function () {

    var hh = this.getHours();
    var mm = this.getMinutes();
    var ss = this.getTime() % 60000;
    ss = (ss - (ss % 1000)) / 1000;
    var clock = hh + ':';
    if (mm < 10) clock += '0';
    clock += mm + ':';
    if (ss < 10) clock += '0';
    clock += ss;

    return clock;
}
Date.prototype.toEpoch = function () {
    if (!this) return null;
    return Math.round(this.getTime() / 1000);
};
Date.fromEpoch = function (epoch) {
    if (!epoch) return null;
    return new Date(epoch * 1000);
};
Date.prototype.getDifferDayCount = function (targetDate) {
    return Math.abs((this - targetDate) / 86400000);
};
Date.prototype.addDays = function (n) {
    this.setDate(this.getDate() + 0 + n);
    return this;
}
Date.prototype.addMonths = function (n) {
    this.setMonth(this.getMonth() + 0 + n);
    return this;
}